package org.tis.tools.senior.module.developer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.senior.module.developer.dao.SRollbackMapper;
import org.tis.tools.senior.module.developer.entity.SRollback;
import org.tis.tools.senior.module.developer.entity.SRollbackList;
import org.tis.tools.senior.module.developer.entity.SStandardList;
import org.tis.tools.senior.module.developer.entity.enums.RollbackStatus;
import org.tis.tools.senior.module.developer.entity.vo.RollbackDetail;
import org.tis.tools.senior.module.developer.exception.DeveloperException;
import org.tis.tools.senior.module.developer.service.ISRollbackListService;
import org.tis.tools.senior.module.developer.service.ISRollbackService;
import org.tis.tools.senior.module.developer.service.ISStandardListService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * sRollback的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/09/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SRollbackServiceImpl extends ServiceImpl<SRollbackMapper, SRollback> implements ISRollbackService {

    @Autowired
    private ISRollbackListService rollbackListService;

    @Autowired
    private ISStandardListService standardListService;

    @Override
    public void affirmRollbackSeccess(Integer guidRollback) {
        SRollback rollback = selectById(guidRollback);
        if (rollback == null){
            throw new DeveloperException("此回退申请不存在！");
        }
        if (rollback.getRollbackStatus().equals(RollbackStatus.COMPLETE)){
            throw new DeveloperException("此回退申请已经成功回退！");
        }

        EntityWrapper<SRollbackList> rollbackEntityWrapper = new EntityWrapper<>();
        rollbackEntityWrapper.eq(SRollbackList.COLUMN_GUID_ROLLBACK, rollback.getGuid());
        List<SRollbackList> rollbackLists = rollbackListService.selectList(rollbackEntityWrapper);
        if (rollbackLists.size() > 0){
            List<String> fullPaths = rollbackLists.stream().map(SRollbackList::getFullPath).collect(Collectors.toList());

            EntityWrapper<SStandardList> standardListEntityWrapper = new EntityWrapper<>();
            standardListEntityWrapper.eq(SStandardList.COLUMN_GUID_WORKITEM, rollback.getGuidWorkitem());
            List<SStandardList> standardLists = standardListService.selectList(standardListEntityWrapper);

            //筛选出标准清单代码与回退代码相同的代码文件
            List<SStandardList> sStandardLists = standardLists.stream().filter(sStandardList ->
                    fullPaths.contains(sStandardList.getFullPath())).collect(Collectors.toList());
            if (sStandardLists.size() > 0){
                standardListService.deleteBatchIds(sStandardLists.stream().map(
                            SStandardList::getGuid).collect(Collectors.toList()));
            }
        }
        //修改回退申请状态为回退成功
        rollback.setRollbackStatus(RollbackStatus.COMPLETE);
        updateById(rollback);
    }

    @Override
    public Page<RollbackDetail> selectRollbackList(Page<RollbackDetail> page,EntityWrapper<RollbackDetail> wrapper) {

        return page.setRecords(this.baseMapper.selectRollbackList(page, wrapper));
    }

    @Override
    public void deleteRollbackBuGuid(Integer guid) {
        SRollback rollback = selectById(guid);
        if (rollback == null){
            throw new DeveloperException("此回退申请不存在！");
        }
        if (rollback.getRollbackStatus().equals(RollbackStatus.COMPLETE)){
            throw new DeveloperException("此回退申请已经成功回退,不允许删除！");
        }
        EntityWrapper<SRollbackList> rollbackListEntityWrapper = new EntityWrapper<>();
        rollbackListEntityWrapper.eq(SRollbackList.COLUMN_GUID_ROLLBACK, guid);
        List<SRollbackList> rollbackLists = rollbackListService.selectList(rollbackListEntityWrapper);
        if(rollbackLists.size() > 0){
            rollbackListService.deleteBatchIds(rollbackLists.stream().map(SRollbackList::getGuid).collect(Collectors.toList()));
        }
        deleteById(rollback);
    }
}

